<?php
namespace Home\Controller;
use Think\Controller;

class WeixinController extends Controller {
    public function index(){
      $redirect_url = urlencode("http://".$_SERVER['SERVER_NAME'].U('home/weixin/callback'));
      var_dump($redirect_url);
      $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.APPID.'&redirect_uri='.$redirect_url.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
      echo $url;
      redirect($url);
    }
    
    public function callback(){
      var_dump($_GET);
      $code = $_GET['code'];
      $login_access_token_api ='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.APPID.'&secret='.APPSECRET.'&code='.$code.'&grant_type=authorization_code';
      $login_access_token_data = json_decode(http_get($login_access_token_api));
      var_dump($login_access_token_data);
       $login_access_token = $login_access_token_data->access_token;
       $openid = $login_access_token_data->openid;
       $user_info_api = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$login_access_token.'&openid='.$openid.'&lang=zh_CN';
       $userinfo = json_decode(http_get($user_info_api));
       var_dump($userinfo);
       $where = array(
         'headimgurl'=>$userinfo->headimgurl,
          'nickname'=>$userinfo->nickname,
          'sex'=>$userinfo->sex,
          'country'=>$userinfo->country,
          'province'=>$userinfo->province,
          'city'=>$userinfo->city,
          'openid'=>$userinfo->openid,

         );
      $admin = M('admin')->where(array('openid'=>$userinfo->openid))->find();
      if (!$admin){
         M('admin')->add($where);
     
      }
       $this->assign('userinfo',(array)$userinfo);
       $this->display();

    }

    public function sentall(){
       $access_token=getAccessToken();
       $url = 'https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token='.$access_token;
       $data = '{
                   "touser":[
                    "of06LxLFzm0oG91CYsWHyleiO2Dc",
                    "of06LxI5d4pTCb9XGf-j1yC6Kd9I"
                   ],
                    "msgtype": "text",
                    "text": { "content": "秋高气爽"}
                }';
        var_dump(http_post($url,$data)) ;        
       /*$toall =  json_decode(http_get($url));
       var_dump($toall);        
*/

    }

    public function sentmessage(){
         $access_token=getAccessToken();
          $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token;
          $message = '{
                 "touser":"of06LxLFzm0oG91CYsWHyleiO2Dc",
                 "template_id":"bajJom6NRh7hrK5hQM6AOJj7835srYCwXCuuxMSM8OI",           
                 "data":{
                         "first": {
                             "value":"签到成功！",
                             "color":"#173177"
                         },
                         "keyword1":{
                             "value":"php高级工程师",
                             "color":"#173177"
                         },
                         "keyword2": {
                             "value":"北京国贸",
                             "color":"#173177"
                         },
                         "keyword3": {
                             "value":"2016年9月19日",
                             "color":"#173177"
                         },
                          "keyword4": {
                             "value":"2016年9月19日",
                             "color":"#173177"
                         },
                         "remark":{
                             "value":"快去撸代码，你个搓比！",
                             "color":"#173177"
                         }
                 }
             }';
         
         var_dump(http_post($url,$message)) ;
         
    }
}